package com.lagou.edu;

import com.alibaba.druid.pool.DruidDataSource;
import com.lagou.edu.context.annotation.Bean;
import com.lagou.edu.context.annotation.ComponentScan;
import com.lagou.edu.context.annotation.Import;
import com.lagou.edu.context.annotation.Value;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * 应用程序配置类
 *
 * @author bwcx_jzy
 * @since 2021/10/21
 */
@ComponentScan("com.lagou.edu")
@Import("/jdbc.properties")
public class ApplicationConfig {

    @Value("jdbc.driver")
    private String driverClassName;

    @Value("jdbc.url")
    private String url;

    @Value("jdbc.username")
    private String username;

    @Value("jdbc.password")
    private String password;

    @Bean
    public DataSource dataSource() throws SQLException {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(this.driverClassName);
        druidDataSource.setUrl(this.url);
        druidDataSource.setUsername(this.username);
        druidDataSource.setPassword(this.password);
        druidDataSource.init();
        return druidDataSource;
    }

    @Override
    public String toString() {
        return "ApplicationConfig{" +
                "driverClassName='" + driverClassName + '\'' +
                ", url='" + url + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}
